package com.cxcs.cxcsojbackendjudgeservice.judge.service.strategy;

import cn.hutool.json.JSONUtil;
import com.cxcs.cxcsojbackendmodel.dto.question.JudgeCase;
import com.cxcs.cxcsojbackendmodel.dto.question.JudgeConfig;
import com.cxcs.cxcsojbackendmodel.dto.questionsubmit.JudgeInfo;
import com.cxcs.cxcsojbackendmodel.entity.Question;
import com.cxcs.cxcsojbackendmodel.enums.JudgeInfoMessageEnum;

import java.util.List;
import java.util.Optional;

/**
 * 默认判题策略
 *
 * @Author: CxCS
 * @Version: 1.0
 * @Date: 2024-08
 */
public class DefaulJudgeStrategy implements JudgeStrategy {
    /**
     *  根据沙箱响应信息 封装成的上下文，匹配用例 是否AC
     * @param judgeContext 判题上下文配置
     * @return JudgeInfo 判题结果
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        // 获取判题上下文
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Question question = judgeContext.getQuestion();

        // 封装返回信息
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(judgeInfo.getMemory());
        judgeInfoResponse.setTime(judgeInfo.getTime());
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.WAITING; // 执行结果

        // 输出数量 与 输入数量不匹配
        if(outputList.size() != inputList.size()) {
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getText());
            return judgeInfoResponse;
        }
        // 依次判断每一项输出 是否正确
        for(int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            // 输出用例 与 代码输出结果 不匹配
            if(judgeCase.getOutput() != outputList.get(i)) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getText());
                return judgeInfoResponse;
            }
        }
        // 判断执行时间内存 等限制
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig questionJudgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class); // 题目执行限制

        Long time = Optional.ofNullable(judgeInfo.getTime()).orElse(0L);
        if(time > questionJudgeConfig.getTimeLimit()) { // 时间限制
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getText());
            return judgeInfoResponse;
        }

        // 代码沙箱对于 执行内存 暂时默认返回空值(待解决)
        // 为空时，默认值 0L
        Long memory = Optional.ofNullable(judgeInfo.getMemory()).orElse(0L);
        if (memory > questionJudgeConfig.getMemoryLimit()) { // 内存限制
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getText());
            return judgeInfoResponse;
        }

        // 判题通过 AC 响应匹配判断结果
        judgeInfoResponse.setMessage(JudgeInfoMessageEnum.ACCEPTED.getText());
        return judgeInfoResponse;
    }
}
